<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="currentChangeHandle(1)">
      <el-form-item>
        <el-input v-model="dataForm.u_code" placeholder="会员码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.in_tracking_number" placeholder="国内运单号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" clearable  placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="currentChangeHandle(1)">查询</el-button>
        <el-button v-if="isAuth('package_storage:v9packagestorage:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button v-if="isAuth('package_storage:v9packagestorage:save')" type="primary" @click="addOrUpdateHandle()">包裹入库</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="inTrackingNumber"
        header-align="center"
        align="center"
        min-width="220"
        label="国内快运单号">
      </el-table-column>
      <el-table-column
        prop="shipping"
        header-align="center"
        align="center"
        min-width="120"
        label="物流公司">
      </el-table-column>
      <el-table-column
        prop="uCode"
        header-align="center"
        align="center"
        min-width="100"
        label="会员识别码">
      </el-table-column>
      <el-table-column
        prop="weightStr"
        header-align="center"
        align="center"
        min-width="80"
        :formatter="formatWeight"
        label="重量">
      </el-table-column>
      <el-table-column
        prop="freightStr"
        header-align="center"
        align="center"
        min-width="80"
        :formatter="formatMoney"
        label="到付运费">
      </el-table-column>
      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        min-width="160"
        :show-overflow-tooltip="true"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="goodsType"
        header-align="center"
        align="center"
        min-width="160"
        :show-overflow-tooltip="true"
        label="类别">
      </el-table-column>
      <el-table-column
        prop="addTimeStr"
        header-align="center"
        align="center"
        min-width="160"
        label="添加时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        min-width="100"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" >入库成功</el-tag>
          <el-tag v-if="scope.row.status === 1" type="success" size="small" >打包中</el-tag>
          <el-tag v-if="scope.row.status === 2" size="small" type="danger">出库成功</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id,scope.row.inTrackingNumber)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './v9packagestorage-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          u_code: '',
          in_tracking_number: '',
          status: ''
        },
        options: [{
          value: 0,
          label: '入库成功'
        }, {
          value: 1,
          label: '已打包'
        }, {
          value: 2,
          label: '出库成功'
        }],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      formatMoney: function (row, column) {
        return row.freightStr ='¥'+ parseFloat(row.freight).toFixed(2) ;
      },
      formatWeight: function (row, column) {
        return row.weightStr = parseFloat(row.weight).toFixed(1) + 'kg';
      },
      formatTime: function (row, column) {
        return row.addTimeStr = this.timestampToTime(row.addTime);
      },
      timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y + M + D + h + m + s;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/package_storage/v9packagestorage/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'u_code': this.dataForm.u_code,
            'in_tracking_number': this.dataForm.in_tracking_number,
            'status': this.dataForm.status
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id,packageid) {
        var ids = packageid ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        let idstr='';
        if(typeof (packageid)!='undefined'){
          idstr=packageid;
        }else {
          idstr=ids.join(',');
        }
        this.$confirm(`确定对[${idstr}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/package_storage/v9packagestorage/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
